package jwdt.xadmin.work.service.impl;

import java.util.List;
import java.util.Map;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.system.bean.TUser;
import jwdt.xadmin.work.bean.TTask;
import jwdt.xadmin.work.bean.TTaskLog;
import jwdt.xadmin.work.service.TaskManageService;

import com.ezerg.db.Jdb;
import com.ezerg.db.ext.PageUtil;
import com.ezerg.util.DateUtil;
import com.ezerg.util.StringUtil;

public class TaskManageServiceImpl implements TaskManageService{
	
	private BaseDao<TTask> baseDao;
	
	public void setBaseDao(BaseDao<TTask> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil doList(PageUtil pu,int projectId,int flag,TUser userInfo) throws Exception{
		String where =" 1=1 ";
		if(projectId !=0){
			where += " and projectid = "+ projectId;
		}
		if(flag == 1){
			where += " and EXISTS (SELECT taskid FROM t_tasklog b WHERE t_task.taskid =b.taskid AND b.userid = "+userInfo.getUserid()+") ";
		}
		String sql = where+" order by taskid asc";
		pu.doList(TTask.class,sql);
		//创造json数据对象
		return pu;
	}
	
	@Override
	public String doAddact(TTask obj,TUser userInfo) throws Exception {
		obj.setCreator(userInfo.getUserid());
		String dateTime = DateUtil.getDateTime();
		obj.setDt(dateTime);
		if(obj.getWorker().intValue()==userInfo.getUserid().intValue()){
			obj.setZt(1);//进行中
			obj.setStarttime(dateTime);
		}else{
			obj.setZt(0);//未开始
		}
		obj.setDepid(userInfo.getDepid());
		String ret = "";
		try {
			ret = baseDao.doAddObj(obj);
			@SuppressWarnings("unchecked")
			List<Map<String, String>> list = Jdb.get().query("select taskid from t_task where dt = '"+dateTime+"'",1);
			if(list !=null){
				int taskId = StringUtil.parseInt(list.get(0).get("taskid"));
				TTaskLog taskLog = new TTaskLog();
				taskLog.setTaskid(taskId);
				taskLog.setName("创建任务："+obj.getName());
				taskLog.setDt(dateTime);
				taskLog.setZt(2);//完成
				taskLog.setUserid(obj.getCreator());
				taskLog.setFromuser(userInfo.getUserid());
				Jdb.get().add(taskLog);
				if(obj.getWorker().intValue()!=userInfo.getUserid().intValue()){
					taskLog.setName(obj.getName());
					taskLog.setUserid(obj.getWorker());
					taskLog.setZt(0);//待签收
					Jdb.get().add(taskLog);
				}
			}
		} catch (Exception e) {
			ret = "添加失败";
		}
		return ret;
	}

	@Override
	public String doEditact(TTask obj, int id) throws Exception {
		obj.setStarttime("".equals(obj.getStarttime())?null:obj.getStarttime());
		obj.setStoptime("".equals(obj.getStarttime())?null:obj.getStarttime());
		return baseDao.doUpdateObj(TTask.class, id, obj);
	}
	
	@Override
	public String doDelact(int id) throws Exception {
		String ret = baseDao.doDelObj(TTask.class, id);
		try {
			Jdb.get().del(TTaskLog.class, "taskid="+id);
		} catch (Exception e) {
			ret = "删除任务日志时失败";
		}
		return ret;
	}

	@Override
	public TTask doLoad(int projectid) throws Exception {
		return baseDao.doLoadObj(TTask.class, projectid);
	}
	
	@Override
	public String doUpdateState(int id, String result) throws Exception {
		return baseDao.doIntUpdateState(TTask.class, " taskid = "+id, result);
	}
}
